#
#   uwsgi.py
#   journalismManger
#
#   Created by luoxh on 2019-07-25-10:21.
#   Copyright © 2019 luoxh. All rights reserved.
#


import os
import sys

try:
    command = sys.argv[1]
except IndexError:
    print('''
        Cannot find the command that needs to be executed, the program provides the following command.
            Start
            Reload
            Stop

        Example: You can type "python uwsgi.py start" in the shell.
    ''')
    sys.exit(1)
else:
    if command.lower() == 'start':
        if os.path.exists('./tmp/journalismManger.pid'):
            print('The project "journalismManger" has already run at the "http://0.0.0.0:5566" address.')
            sys.exit(0)
        if os.path.exists('./tmp'): os.system('rm -rf ./tmp/*')
        os.system('touch ./tmp/journalismManger.log')
        os.system('./env/bin/uwsgi ./conf/uWSGI.ini --thunder-lock')
        print('The project "journalismManger" has already run at the "http://0.0.0.0:5566" address.')
        sys.exit(1)
    if command.lower() == 'reload':
        os.system('./env/bin/uwsgi --reload ./tmp/journalismManger.pid')
        sys.exit(1)
    if command.lower() == 'stop':
        os.system('ps ax|grep uwsgi')
        os.system('rm -rf ./tmp/journalismManger.pid')
        os.system('killall -9 uwsgi')
        sys.exit(1)
    if command.lower() == '--help':
        print('''
                We have provided the following commands:
                Start
                Reload
                Stop
                
                Example: If you want to start this program, use "python uwsgi.py start".
            ''')
        sys.exit(1)

    else:
        print(f'''
                The command you entered "{command}" has a problem.
                Please use --help to view the usage of the file.
                ''')
        sys.exit(1)
